/**
 * Created by Administrator on 2017/3/27 0027.
 */
var arr_texture=[];
var longpicMap;
var tip_textrue;
var bgMap;
var scene,camera,renderer,controls;
var ccbl=false;
var goFinal=false;
Main={
    Create:function(){
        var main={};

        scene=new THREE.Scene();
        camera=new THREE.PerspectiveCamera(70,1136/740,1,10000);
        renderer=new THREE.WebGLRenderer({antialias:true});
        renderer.domElement.style.position='absolute';

        document.getElementById('container').appendChild(renderer.domElement);
        renderer.setPixelRatio(window.devicePixelRatio ? window.devicePixelRatio : 1);
        renderer.setSize( window.innerWidth, window.innerHeight );

        var light=new THREE.AmbientLight(0xff0000,4);
        scene.add(light);

        controls = new THREE.OrbitControls( camera, renderer.domElement );
        controls.rotateSpeed = 0.5;
        controls.minDistance = 0;
        controls.maxDistance = 6000;
   // controls.addEventListener( 'change', controlsChange );



        console.log("123");
        var hajime=Hajime.Create();
        var CC;
        document.addEventListener('HajimeCompleted',HajimeCompleted);
        function HajimeCompleted(e){
            CC=ControlCenter.Create();
            CC.Go();
        }
        document.addEventListener('CameraChange',cameraChange);
        document.addEventListener('CameraChange2',cameraChange2);
        function cameraChange2()
        {
            camera.fov=70;
        }
        function cameraChange()
        {
            camera.fov=130;
        }


        //****************
        //竖直状态
        camera.rotation.z=Math.PI/180*90;
        camera.position.set(0,0,810);
        //****************
        //****************
        //横屏状态
        // camera.position.set(0,0,810);
        //****************


        onWindowResize();
        window.addEventListener( 'resize', onWindowResize, false );
        function onWindowResize() {


            camera.aspect = window.innerWidth / window.innerHeight;
            if(window.innerWidth / window.innerHeight>1){
                document.dispatchEvent(new CustomEvent("wrongDirection",{detail:0}));
            }
            else{
                document.dispatchEvent(new CustomEvent("wrongDirection",{detail:1}));
            }
            // camera.updateProjectionMatrix();
            // renderer.setSize( window.innerWidth, window.innerHeight );
        }


        // window.addEventListener("deviceorientation", orientationHandler, false);
        window.addEventListener("bala",function(){
            goFinal=true;
        })
        // function orientationHandler(event) {
        //     if(!goFinal) {
        //         aa = event.alpha;
        //         bb = event.beta;
        //         gg = event.gamma;
        //         if (Math.abs(bb) < 179 && camera != null) {
        //             camera.position.x = bb * 4;
        //             camera.position.z = 810 - Math.abs(bb) * 4;
        //         }
        //         var yy = camera.position.y;
        //         var yyG=-gg;
        //
        //         // if(Math.abs(yyG-yy)<5) {
        //             document.dispatchEvent(new CustomEvent("getText", {detail:222}));
        //             if (camera != null) {
        //                 // camera.position.y = -gg;
        //             }
        //         // }
        //     }
        // }


        animate();
        function animate(){
            requestAnimationFrame(animate);
            renderer.render(scene,camera);
            // controls.update();
            camera.updateProjectionMatrix();


            if(CC) {
                CC.DoIt();
            }

        }
        function controlsChange(){
            console.log(camera.position);
        }


        return main;
    }
}